package utils;

import controller.DBUtils;
import controller.clothes;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ShowCloth extends HttpServlet {
    Connection connection=null;
    Statement statement = null;
    ResultSet resultSet = null;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String str = "select * from clothes";
        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        //System.out.println("asqweqw");
        try {
            connection = DBUtils.Connection();
            statement = connection.createStatement();

            resultSet = statement.executeQuery(str);
            List<String> list = new LinkedList();
            StringBuffer strr = new StringBuffer();
            strr.append("{");
            while (resultSet.next()){
//                clothes clothes = new clothes();
//                clothes.setCloth_path(resultSet.getString("cloth_path"));
//                clothes.setCloth_color(resultSet.getString("cloth_color"));
//                clothes.setCloth_name(resultSet.getString("cloth_name"));
//                int i = resultSet.getInt("cloth_number");
//                clothes.setCloth_number(i);
//                int j = resultSet.getInt("cloth_id");
//                clothes.setCloth_id(j);
//                clothes.setCloth_price(resultSet.getString("cloth_price"));
//                list.addAll()

                String cloth_id = resultSet.getString("cloth_id");
                String cloth_path = resultSet.getString("cloth_path");
                String cloth_price = resultSet.getString("cloth_price");
                String cloth_name = resultSet.getString("cloth_name");
                strr.append("\""+cloth_id+"\":{\"cloth_path\":"+"\""+cloth_path+"\""+",\"cloth_price\":"+"\""+cloth_price+"\""+",\"cloth_name\":"+"\""+cloth_name+"\""+"},");
            }
            int length = strr.length();
            strr.delete(length-1,length);
            strr.append("}");
           // System.out.println(strr);
            resp.getWriter().write(String.valueOf(strr));


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }





}
